package com.xgp.company.创建型模式.第一种_单例模式.饿汉式;

/**
 *
 * 核心：保证一个类只有一个实例，并且提供一个范围该实例的全局访问点
 */
public class SingletonDemo01 {

    //保证类只有一个实例，私有其构造器
    private SingletonDemo01() {

    }

    //创建自身对象
    private static SingletonDemo01 instance = new SingletonDemo01();

    //全局公开的方法
    public static SingletonDemo01 getInstance() {
        return instance;
    }

}

class SingletonDemo01Test {
    public static void main(String[] args) {
        SingletonDemo01 instance = SingletonDemo01.getInstance();
        SingletonDemo01 instance2 = SingletonDemo01.getInstance();

        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
        System.out.println(instance == instance2);
    }
}
